package com.dayu.web.controller.recovery;

import java.util.List;

import com.dayu.common.annotation.RepeatSubmit;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.dayu.common.annotation.Log;
import com.dayu.common.core.controller.BaseController;
import com.dayu.common.core.domain.AjaxResult;
import com.dayu.common.enums.BusinessType;
import com.dayu.recovery.domain.RecoveryScattered;
import com.dayu.recovery.service.IRecoveryScatteredService;
import com.dayu.common.utils.poi.ExcelUtil;
import com.dayu.common.core.page.TableDataInfo;

/**
 * 散户验机Controller
 * 
 * @author HK
 * @date 2021-04-16
 */
@RestController
@RequestMapping("/recovery/scattered")
public class RecoveryScatteredController extends BaseController
{
    @Autowired
    private IRecoveryScatteredService recoveryScatteredService;

    /**
     * 查询散户验机列表
     */
    @PreAuthorize("@ss.hasPermi('recovery:scattered:list')")
    @GetMapping("/list")
    public TableDataInfo list(RecoveryScattered recoveryScattered)
    {
        startPage();
        List<RecoveryScattered> list = recoveryScatteredService.selectRecoveryScatteredList(recoveryScattered);
        return getDataTable(list);
    }

    /**
     * 导出散户验机列表
     */
    @PreAuthorize("@ss.hasPermi('recovery:scattered:export')")
    @Log(title = "散户验机", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(RecoveryScattered recoveryScattered)
    {
        List<RecoveryScattered> list = recoveryScatteredService.exportScatteredList(recoveryScattered);
        ExcelUtil<RecoveryScattered> util = new ExcelUtil<RecoveryScattered>(RecoveryScattered.class);
        return util.exportExcel(list, "scattered");
    }

    /**
     * 获取散户验机详细信息
     */
    @PreAuthorize("@ss.hasPermi('recovery:scattered:query')")
    @GetMapping(value = "/{scatteredId}")
    public AjaxResult getInfo(@PathVariable("scatteredId") Long scatteredId)
    {
        return AjaxResult.success(recoveryScatteredService.selectRecoveryScatteredById(scatteredId));
    }

    /**
     * 新增散户验机
     */
    @RepeatSubmit
    @PreAuthorize("@ss.hasPermi('recovery:scattered:add')")
    @Log(title = "散户验机", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RecoveryScattered recoveryScattered)
    {
        return toAjax(recoveryScatteredService.insertRecoveryScattered(recoveryScattered));
    }

    /**
     * 修改散户验机
     */
    @PreAuthorize("@ss.hasPermi('recovery:scattered:edit')")
    @Log(title = "散户验机", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RecoveryScattered recoveryScattered)
    {
        return toAjax(recoveryScatteredService.updateRecoveryScattered(recoveryScattered));
    }

    /**
     * 删除散户验机
     */
    @PreAuthorize("@ss.hasPermi('recovery:scattered:remove')")
    @Log(title = "散户验机", businessType = BusinessType.DELETE)
	@DeleteMapping("/{scatteredIds}")
    public AjaxResult remove(@PathVariable Long[] scatteredIds)
    {
        return toAjax(recoveryScatteredService.deleteRecoveryScatteredByIds(scatteredIds));
    }

    @RepeatSubmit
    @PreAuthorize("@ss.hasPermi('recovery:scattered:collect')")
    @Log(title = "回收散户验机", businessType = BusinessType.INSERT)
    @PostMapping("/collect/goods")
    public AjaxResult collectGoods(@RequestBody RecoveryScattered scattered) {
        RecoveryScattered recoveryScattered = recoveryScatteredService.selectRecoveryScatteredById(scattered.getScatteredId());
        if(recoveryScattered.getStatus() == 1) {
            return AjaxResult.error("此数据已经生成过了，请勿重复生成！");
        }
        return toAjax(recoveryScatteredService.collectScatteredGoods(scattered));
    }
}
